package com.cy.pj.notice.service.impl;

import com.cy.pj.notice.dao.SysNoticeDao;
import com.cy.pj.notice.pojo.SysNotice;
import com.cy.pj.notice.service.SysNoticeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

//调用Dao层(数据层)的方法.
//service实现类耦合与Dao接口
@Service  //@Componet  功能相同时通用的 ,但标识不同
public class SysNoticeServiceImpl implements SysNoticeService {
    //初始化日志对象 接口=接口的实现(实现类)
    //日志API SLF4J
   private static final Logger log = LoggerFactory.getLogger(SysNoticeServiceImpl.class);

   @Autowired  //此位置省略,必须添加构造方法;
    private SysNoticeDao sysNoticeDao;
    //当不适用@autowired自动注入,可以使用构造方法直接进行DI操作;
//    @Autowired  //可以省略
//    public SysNoticeServiceImpl(SysNoticeDao sysNoticeDao) {
//        this.sysNoticeDao=sysNoticeDao;
//    }

    @Override
    public List<SysNotice> findNotices(SysNotice sysNotice) {
        //
        long t1 = System.currentTimeMillis();
        log.info("method start {}",t1);//{}表示占位符,逗号右边传到左边;
        List<SysNotice> list = sysNoticeDao.selectNotice(sysNotice);
        long t2 = System.currentTimeMillis();
        log.info("method end {}",t2);//{}表示占位符
        return list;
    }

    @Override
    public int saveNotice(SysNotice sysNotice) {
        int rows = sysNoticeDao.insertNotice(sysNotice);
        return rows;
    }

    @Override
    public int updateNotice(SysNotice sysNotice) {
        return sysNoticeDao.updateNotice(sysNotice);
    }

    @Override
    public SysNotice findById(Long id) {
        return sysNoticeDao.findById(id);
    }

    @Override
    public int deleteById(@PathVariable Long... ids) {
        int rows = sysNoticeDao.deleteById(ids);
        if (rows == 0)
            throw new RuntimeException("记录可能已经不存在了");
//            throw new ServiceException("记录可能已经不存在了");
        return rows;
    }

}
